﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Jemgine.MapEditor
{
	class GearFactory
	{
		private static float Sin(float angle)
		{
			return (float)System.Math.Sin(angle);
		}

		private static float Cos(float angle)
		{
			return (float)System.Math.Cos(angle);
		}

		public static Jemgine.Data.EditorPolygon CreateGear(float radius, int numberOfTeeth, float tipPercentage, float toothHeight)
		{
			var vertices = new Jemgine.Data.EditorPolygon();

			float stepSize = MathHelper.TwoPi / numberOfTeeth;

			float toothTipStepSize = (stepSize / 2f) * tipPercentage;

			float toothAngleStepSize = (stepSize - (toothTipStepSize * 2f)) / 2f;

			for (int i = 0; i < numberOfTeeth; i++)
			{
				vertices.Add(new Vector2((radius) * Cos(stepSize * i),
					-(radius) * Sin(stepSize * i)));

				vertices.Add(new Vector2((radius + toothHeight) * Cos((stepSize * i) + toothAngleStepSize),
					-(radius + toothHeight) * Sin((stepSize * i) + toothAngleStepSize)));

				vertices.Add(new Vector2((radius + toothHeight) * Cos((stepSize * i) + toothAngleStepSize + toothTipStepSize),
					-(radius + toothHeight) * Sin((stepSize * i) + toothAngleStepSize + toothTipStepSize)));

				vertices.Add(new Vector2((radius) * Cos((stepSize * i) + (toothAngleStepSize * 2f) + toothTipStepSize),
					-(radius) * Sin((stepSize * i) + (toothAngleStepSize * 2f) + toothTipStepSize)));
			}

			return vertices;
		}
	}
}
